package com.hyfrogx.common.base;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;
import java.util.Map;

/**
 * 基础的查询表单参数
 *
 * @author wanglei
 * @date 2019-06-05 16:58:54
 */
@Data
@ApiModel("基础的查询表单参数")
public class QueryForm<T> {

	@ApiModelProperty(value = "分页")
	private BasePageForm page;

	@ApiModelProperty(value = "排序")
	private BaseOrderForm order;

	@ApiModelProperty(value = "查询参数")
	private Map params;

	public IPage<T> getIPage() {
		//分页对象
		Page<T> iPage = new Page<>();
		if (ObjectUtil.isNull(page)) {
			return iPage;
		}

		iPage.setCurrent(page.getCurrent());
		iPage.setSize(page.getSize());

		String[] asc = getAscArray();
		String[] desc = getDescArray();
		if (ArrayUtil.isNotEmpty(asc)) {
			iPage.setAsc(asc);
		}
		if (ArrayUtil.isNotEmpty(desc)) {
			iPage.setDesc(desc);
		}
		return iPage;
	}

	private String[] getAscArray() {
		List<String> asc = CollUtil.newArrayList();
		if (ObjectUtil.isNotNull(order)) {
			if (CollUtil.isNotEmpty(order.getAscT())) {
				asc.addAll(order.getAscT());
			}
			if (CollUtil.isNotEmpty(order.getAsc())) {
				for (String key : order.getAsc()) {
					asc.add(StrUtil.toUnderlineCase(key));
				}
			}
		}
		//去空
		CollUtil.removeBlank(asc);
		//去重
		CollUtil.distinct(asc);
		return ArrayUtil.toArray(asc, String.class);
	}

	private String[] getDescArray() {
		List<String> desc = CollUtil.newArrayList();
		if (ObjectUtil.isNotNull(order)) {
			if (CollUtil.isNotEmpty(order.getDescT())) {
				desc.addAll(order.getAscT());
			}
			if (CollUtil.isNotEmpty(order.getDesc())) {
				for (String key : order.getDesc()) {
					desc.add(StrUtil.toUnderlineCase(key));
				}
			}
		}
		//去空
		CollUtil.removeBlank(desc);
		//去重
		CollUtil.distinct(desc);
		return ArrayUtil.toArray(desc, String.class);
	}

	public QueryWrapper<T> getWrapper(boolean orderTag) {
		QueryWrapper<T> wrapper = new QueryWrapper<T>();
		/*
		if (ObjectUtil.isNull(params)) {
			return wrapper;
		}

		Map likeMap = (Map) BeanUtil.getProperty(params, "like");
		if (CollUtil.isNotEmpty(likeMap)) {
			for (Object key : likeMap.keySet()) {
				if (ObjectUtil.isNotNull(key)) {
					wrapper.like(ObjectUtil.isNotNull(likeMap.get(key)), StrUtil.toUnderlineCase(Convert.toStr(key)), likeMap.get(key));
				}
			}
		}
		 */

		if (orderTag) {
			String[] asc = getAscArray();
			String[] desc = getDescArray();
			if (ArrayUtil.isNotEmpty(asc)) {
				wrapper.orderByAsc(asc);
			}
			if (ArrayUtil.isNotEmpty(desc)) {
				wrapper.orderByDesc(desc);
			}
		}

		return wrapper;
	}

	public Object getParams(String key) {
		if (CollUtil.isEmpty(params)) {
			return null;
		}
		return params.get(key);
	}

	public String getParamStr(String key) {
		return Convert.toStr(getParams(key));
	}

	public void setParams(String key, Object value) {
		if (CollUtil.isEmpty(params)) {
			params = CollUtil.newHashMap();
		}
		params.put(key, value);
	}

	public Long getParamLong(String key) {
		return Convert.toLong(getParams(key));
	}

	public Integer getParamInteger(String key) {
		return Convert.toInt(getParams(key));
	}

	public Boolean getParamBoolean(String key) {
		return Convert.toBool(getParams(key));
	}
}
